package com.zq.zhufengfm.adapter;

/**
 * Project_name : ZhuFengFM
 * Author : zhaoQiang
 * Date : 2015/10/21,15:25
 * Email : zhaoq_hero@163.com
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.squareup.picasso.Picasso;
import com.zq.zhufengfm.entity.discoverrecommened.AlbumRecommend;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendAlubums;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendColumns;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendItem;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendSpecial;
import com.zq.zhufengfm.entity.discoverrecommened.SpecialItem;
import com.zq.zhufengfm.widgets.SpecialItemView;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 发现 推荐部分的listView adapter
 * 支持多布局的复用
 */
public class DiscoverRecommendAdapter extends BaseAdapter{
    private Context context;
    private List<DiscoverRecommendItem> items;
    private BitmapUtils bitmapUtils;

    //-----------------------------------

    //点击事件处理器：
    /**
     * 推荐专辑的点击事件
     */
    private View.OnClickListener onRecommendAlbumClickListener;

    //-------------------------------


    public void setOnRecommendAlbumClickListener(View.OnClickListener onRecommendAlbumClickListener) {
        this.onRecommendAlbumClickListener = onRecommendAlbumClickListener;
    }

    public DiscoverRecommendAdapter( Context context,List<DiscoverRecommendItem> items) {
        this.items = items;
        this.context = context;

        bitmapUtils = new BitmapUtils(context);
        initBitmapUtils();
    }

    private void initBitmapUtils() {
        //初始化  Bitmap
        //设置加载时的图片
        bitmapUtils.configDefaultLoadingImage(android.R.drawable.stat_sys_download);
        //设置默认加载失败的图片
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.ic_launcher);
        bitmapUtils.configDefaultConnectTimeout(5000);

    }

    @Override
    public int getCount() {
        int ret =0;
        if (items != null) {
            ret = items.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    /**
     * 返回当前   需要创建的视图类型
     * @return
     */
    @Override
    public int getViewTypeCount() {
        return 3;//当前listView  可以显示三种类型  小编推荐视图，精品清单视图，发现新奇视图
    }

    /**
     * 获取当前将被getView  创建的item的类型  为小编推荐，，，。。。。
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        int ret = 0;

        DiscoverRecommendItem item = items.get(position);

        if (item instanceof DiscoverRecommendAlubums) {//小编推荐

            ret = 2;

        }else if(item instanceof DiscoverRecommendColumns){//发现新奇

            ret = 1;

        }else if(item instanceof DiscoverRecommendSpecial){//精品清单

            ret = 0;
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //1，获取数据：
        View ret = null;

        DiscoverRecommendItem item = items.get(position);

        if (item instanceof DiscoverRecommendAlubums) {//小编推荐

            ret = bindAlbumsView(position,convertView,parent);

        }else if(item instanceof DiscoverRecommendColumns){//发现新奇

            ret = bindColumView(position,convertView,parent);

        }else if(item instanceof DiscoverRecommendSpecial){//精品推荐

            ret = bindSpecialView(position,convertView,parent);

        }
        return ret;
    }

    /**
     * 加载专辑的推荐deItem 小编推荐  和热门推荐
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindAlbumsView(int position,View convertView,ViewGroup parent){
        View ret = null;

        //1 复用
        if (ret != null) {
            ret = convertView;
        }else{

            LayoutInflater inflater = LayoutInflater.from(context);

            ret = inflater.inflate(R.layout.discover_recommend_album_item
                                    ,parent,false
            );
        }

        //2 ViewHolder ：
        AlbumViewHolder holder = (AlbumViewHolder) ret.getTag();

        if (holder == null) {

            holder = new AlbumViewHolder();

            holder.txtTitle = (TextView) ret.findViewById(R.id.recommend_album_title);
            holder.txtMore = (TextView) ret.findViewById(R.id.recommend_album_more);

            //TODO 点击  more 进入推荐列表

            holder.albumIcons = new ImageView[3];
            holder.albumNames = new TextView[3];
            holder.trackNames = new TextView[3];

            for (int i = 0; i < 3; i++) {
                //给holder设置
                holder.albumIcons[i] = (ImageView) findView(ret,"recommend_album_icon_"+i);

                    //TODO 点击专辑图片  进入专辑详细详情
                    holder.albumIcons[i].setOnClickListener(onRecommendAlbumClickListener);

                holder.albumNames[i] = (TextView) findView(ret,"recommend_album_name_"+i);

                holder.trackNames[i] = (TextView) findView(ret,"recommend_album_trackName_"+i);
                    //点击专辑图片下的字体  也进入专辑详情
                     holder.trackNames[i].setOnClickListener(onRecommendAlbumClickListener);
            }
            ret.setTag(holder);
        }
        //3，获取数据设置内容：
        DiscoverRecommendAlubums albums = (DiscoverRecommendAlubums) items.get(position);

//        绑定数据
        String title = albums.getTitle();
        holder.txtTitle.setText(title);

        //判断是否有更多：
        if(!albums.isHasMore()){
            holder.txtMore.setVisibility(View.INVISIBLE);
        }
        //处理专辑标题
        List<AlbumRecommend> albumList = albums.getAlbumRecommendsList();

        if (albumList != null) {
            for (int i = 0; i < holder.albumNames.length; i++) {
                AlbumRecommend recommend = albumList.get(i);
                holder.albumNames[i].setText(recommend.getTitle());//获取专辑名称
                holder.trackNames[i].setText(recommend.getTrackTitle());//获取推荐曲目的名称

                ImageView imageView = holder.albumIcons[i];

                //设置标记：
                imageView.setTag(recommend);//设置点击事件
                holder.albumNames[i].setTag(recommend);//获取专辑名称
                holder.trackNames[i].setTag(recommend);//设置点击事件

                //使用picasso加载图片
                Picasso.with(context)           //创建实例
                        .load(recommend.getCoverLarge())    //加载网址
                        .resize(150, 150)
                        .centerCrop()               //设置  靠中心点放大  并裁剪多余部分
                        .into(imageView);

                //使用 xUtil 调用
//                bitmapUtils.display(holder.albumIcons[i],recommend.getCoverLarge());

            }
        }
        return ret;
    }
    /**
     * 加载发现  新奇
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindColumView(int position,View convertView,ViewGroup parent){

        return null;

    }
    /**
     * 加载   精品清单
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindSpecialView(int position,View convertView,ViewGroup parent){

        View ret = null;
        //1，复用
        if (convertView != null) {
            ret = convertView;
        }else{
            LayoutInflater inflater = LayoutInflater.from(context);

            ret = inflater.inflate(R.layout.discover_recommend_special_item
            ,parent,false);
        }

        //2,ViewHolder处理
        SpecialViewHolder holder = (SpecialViewHolder) ret.getTag();
        if (holder == null) {
            holder = new SpecialViewHolder();

            holder.txtTitle = (TextView) ret.findViewById(R.id.recommend_specila_title);
            holder.txtMore = (TextView) ret.findViewById(R.id.recommend_specila_more);

            holder.itemContainer = (LinearLayout) ret.findViewById(R.id.recommend_specila_container);

            ret.setTag(holder);
        }

        //3,数据显示：
        DiscoverRecommendSpecial special = (DiscoverRecommendSpecial) items.get(position);

        holder.txtTitle.setText(special.getTitle());

        if (!special.isHasMore()) {
            holder.txtMore.setVisibility(View.INVISIBLE);
        }

        //3.1  清空旧的 LinearLayout的数据  再根据  听单的item来添加
        holder.itemContainer.removeAllViews();

        List<SpecialItem> specialItems = special.getSpecialItems();

        if (specialItems != null) {

            int index = 0;
            int itemCount = specialItems.size();

            for (SpecialItem specialItem : specialItems){

                index++;

                //创建自定义的控件     View     用来显示听单信息
                SpecialItemView itemView = new SpecialItemView(context);

                LinearLayout.LayoutParams lp = new
                        LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                );

                itemView.setLayoutParams(lp);

                holder.itemContainer.addView(itemView);

                //设置听单内容
                itemView.setTitle(specialItem.getTitle());

                itemView.setSubTitle(specialItem.getSubtitle());

                itemView.setNumber(specialItem.getFootnote());
                //判断是否是最后一个：
                itemView.setShowBottomLine(true);
                //获取听单的  图标Imageview
                ImageView imageView = itemView.getImgIcon();
                String coverPath = specialItem.getCoverPath();
                //加载图片：
                Picasso.with(context).load(coverPath).into(imageView);
                if(index  ==  itemCount - 1){
                    //最后一个
                    itemView.setShowBottomLine(false);
                }
            }
        }
        return ret;
    }

    /**
     * 利用类的反射来获取   指定R.id.XXXX代表View
     * @param container
     * @param fieldName
     * @return
     */
    public static View findView(View container,String fieldName){
        View ret = null;

        if (container != null && fieldName!=null) {
            Class<R.id> idClass = R.id.class;

            try {
                Field field = idClass.getDeclaredField(fieldName);
                int id = field.getInt(idClass);
                //通过静态常量  获取整形int值  来查找View
                ret = container.findViewById(id);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 精品听单  ViewHolder
     */
    private static class SpecialViewHolder{
        public TextView txtTitle;
        public TextView txtMore;

        //存储  SpecialItemView：
        public LinearLayout itemContainer;
    }

    /**
     * 小编推荐的  ViewHolder
     */
    private static class AlbumViewHolder{

        public TextView txtTitle;
        public TextView txtMore;

        //三张图片，，在不同的relativLayout
        public ImageView[] albumIcons;

        public TextView[] albumNames;//三个专辑标题

        public TextView[] trackNames;//三个曲目名称

    }

}
